home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / chpass / RCS / field.c,v < prev    next >
Encoding:
Text File  |  1990-07-12  |  5.0 KB  |  270 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.07.11.18.15.35;  author shirriff;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/*
  26.  * Copyright (c) 1988 The Regents of the University of California.
  27.  * All rights reserved.
  28.  *
  29.  * Redistribution and use in source and binary forms are permitted
  30.  * provided that the above copyright notice and this paragraph are
  31.  * duplicated in all such forms and that any documentation,
  32.  * advertising materials, and other materials related to such
  33.  * distribution and use acknowledge that the software was developed
  34.  * by the University of California, Berkeley.  The name of the
  35.  * University may not be used to endorse or promote products derived
  36.  * from this software without specific prior written permission.
  37.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  38.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  39.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  40.  */
  41.  
  42. #ifndef lint
  43. static char sccsid[] = "@@(#)field.c    5.8 (Berkeley) 3/16/89";
  44. #endif /* not lint */
  45.  
  46. #include <sys/param.h>
  47. #include <pwd.h>
  48. #include <grp.h>
  49. #include <strings.h>
  50. #include <stdio.h>
  51. #include <ctype.h>
  52. #include <chpass.h>
  53. #include "pathnames.h"
  54.  
  55. /* ARGSUSED */
  56. p_login(p, pw, ep)
  57.     char *p;
  58.     struct passwd *pw;
  59.     struct entry *ep;
  60. {
  61.     if (!*p) {
  62.         (void)fprintf(stderr, "chpass: empty login field.\n");
  63.         return(1);
  64.     }
  65.     if (*p == '-') {
  66.         (void)fprintf(stderr,
  67.             "chpass: login names may not begin with a hyphen.\n");
  68.         return(1);
  69.     }
  70.     if (!(pw->pw_name = strdup(p))) {
  71.         (void)fprintf(stderr, "chpass: can't save entry.\n");
  72.         return(1);
  73.     }
  74.     if (index(p, '.'))
  75.         (void)fprintf(stderr,
  76.             "chpass: \'.\' is dangerous in a login name.\n");
  77.     for (; *p; ++p)
  78.         if (isupper(*p)) {
  79.             (void)fprintf(stderr,
  80.                 "chpass: upper-case letters are dangerous in a login name.\n");
  81.             break;
  82.         }
  83.     return(0);
  84. }
  85.  
  86. /* ARGSUSED */
  87. p_passwd(p, pw, ep)
  88.     char *p;
  89.     struct passwd *pw;
  90.     struct entry *ep;
  91. {
  92.     if (!*p)
  93.         pw->pw_passwd = "";    /* "NOLOGIN"; */
  94.     else if (!(pw->pw_passwd = strdup(p))) {
  95.         (void)fprintf(stderr, "chpass: can't save password entry.\n");
  96.         return(1);
  97.     }
  98.     
  99.     return(0);
  100. }
  101.  
  102. /* ARGSUSED */
  103. p_uid(p, pw, ep)
  104.     register char *p;
  105.     struct passwd *pw;
  106.     struct entry *ep;
  107. {
  108.     int id;
  109.  
  110.     if (!*p) {
  111.         (void)fprintf(stderr, "chpass: empty uid field.\n");
  112.         return(1);
  113.     }
  114.     if (!isdigit(*p)) {
  115.         (void)fprintf(stderr, "chpass: illegal uid.\n");
  116.         return(1);
  117.     }
  118.     id = atoi(p);
  119.     if ((u_int)id > USHRT_MAX) {
  120.         (void)fprintf(stderr, "chpass: %d > max uid value (%d).\n",
  121.             id, USHRT_MAX);
  122.         return(1);
  123.     }
  124.     pw->pw_uid = id;
  125.     return(0);
  126. }
  127.  
  128. /* ARGSUSED */
  129. p_gid(p, pw, ep)
  130.     register char *p;
  131.     struct passwd *pw;
  132.     struct entry *ep;
  133. {
  134.     struct group *gr;
  135.     int id;
  136.  
  137.     if (!*p) {
  138.         (void)fprintf(stderr, "chpass: empty gid field.\n");
  139.         return(1);
  140.     }
  141.     if (!isdigit(*p)) {
  142.         if (!(gr = getgrnam(p))) {
  143.             (void)fprintf(stderr,
  144.                 "chpass: unknown group %s.\n", p);
  145.             return(1);
  146.         }
  147.         pw->pw_gid = gr->gr_gid;
  148.         return(0);
  149.     }
  150.     id = atoi(p);
  151.     if ((u_int)id > USHRT_MAX) {
  152.         (void)fprintf(stderr, "chpass: %d > max gid value (%d).\n",
  153.             id, USHRT_MAX);
  154.         return(1);
  155.     }
  156.     pw->pw_gid = id;
  157.     return(0);
  158. }
  159.  
  160. /* ARGSUSED */
  161. p_class(p, pw, ep)
  162.     char *p;
  163.     struct passwd *pw;
  164.     struct entry *ep;
  165. {
  166.     if (!*p)
  167.         pw->pw_class = "";
  168.     else if (!(pw->pw_class = strdup(p))) {
  169.         (void)fprintf(stderr, "chpass: can't save entry.\n");
  170.         return(1);
  171.     }
  172.     
  173.     return(0);
  174. }
  175.  
  176. /* ARGSUSED */
  177. p_change(p, pw, ep)
  178.     char *p;
  179.     struct passwd *pw;
  180.     struct entry *ep;
  181. {
  182.     if (!atot(p, &pw->pw_change))
  183.         return(0);
  184.     (void)fprintf(stderr, "chpass: illegal date for change field.\n");
  185.     return(1);
  186. }
  187.  
  188. /* ARGSUSED */
  189. p_expire(p, pw, ep)
  190.     char *p;
  191.     struct passwd *pw;
  192.     struct entry *ep;
  193. {
  194.     if (!atot(p, &pw->pw_expire))
  195.         return(0);
  196.     (void)fprintf(stderr, "chpass: illegal date for expire field.\n");
  197.     return(1);
  198. }
  199.  
  200. /* ARGSUSED */
  201. p_gecos(p, pw, ep)
  202.     char *p;
  203.     struct passwd *pw;
  204.     struct entry *ep;
  205. {
  206.     if (!*p)
  207.         ep->save = "";
  208.     else if (!(ep->save = strdup(p))) {
  209.         (void)fprintf(stderr, "chpass: can't save entry.\n");
  210.         return(1);
  211.     }
  212.     return(0);
  213. }
  214.  
  215. /* ARGSUSED */
  216. p_hdir(p, pw, ep)
  217.     char *p;
  218.     struct passwd *pw;
  219.     struct entry *ep;
  220. {
  221.     if (!*p) {
  222.         (void)fprintf(stderr, "chpass: empty home directory field.\n");
  223.         return(1);
  224.     }
  225.     if (!(pw->pw_dir = strdup(p))) {
  226.         (void)fprintf(stderr, "chpass: can't save entry.\n");
  227.         return(1);
  228.     }
  229.     return(0);
  230. }
  231.  
  232. /* ARGSUSED */
  233. p_shell(p, pw, ep)
  234.     register char *p;
  235.     struct passwd *pw;
  236.     struct entry *ep;
  237. {
  238.     register char *sh, *t;
  239.     char *getusershell();
  240.  
  241.     if (!*p) {
  242.         pw->pw_shell = _PATH_BSHELL;
  243.         return(0);
  244.     }
  245.     setusershell();
  246.     for (;;) {
  247.         if (!(sh = getusershell())) {
  248.             /* only admin can set "restricted" shells */
  249.             if (!uid)
  250.                 break;
  251.             (void)fprintf(stderr,
  252.                 "chpass: %s: non-standard shell.\n", p);
  253.             return(1);
  254.         }
  255.         if (!strcmp(p, sh))
  256.             break;
  257.         /* allow just shell name */
  258.         if ((t = rindex(sh, '/')) && !strcmp(p, t)) {
  259.             p = t;
  260.             break;
  261.         }
  262.     }
  263.     if (!(pw->pw_shell = strdup(p))) {
  264.         (void)fprintf(stderr, "chpass: can't save entry.\n");
  265.         return(1);
  266.     }
  267.     return(0);
  268. }
  269. @
  270.